package com.pb.camera.more;

import com.pb.camera.R;
import com.pb.camera.manager.CameraSet;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class TimePickActivity extends Activity {
	private TimePicker mTimePickerStart;
	private TimePicker mTimePickerEnd;
	private int mFromHourTime;
	private int mFromMinTime;
	private int mEndHourTime;
	private int mEndMinTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		initView();
		initData();
		initListener();
	}

	private void initData() {
		if (CameraSet.recodingStartTime != null) {
			mFromHourTime = Integer.parseInt(CameraSet.recodingStartTime
					.split(":")[0]);
			mFromMinTime = Integer.parseInt(CameraSet.recodingStartTime
					.split(":")[1]);
		}
		if (CameraSet.recodingEndTime != null) {
			mEndHourTime = Integer.parseInt(CameraSet.recodingEndTime
					.split(":")[0]);
			mEndMinTime = Integer
					.parseInt(CameraSet.recodingEndTime.split(":")[1]);
		}
		mTimePickerStart.setCurrentHour(mFromHourTime);
		mTimePickerStart.setCurrentMinute(mFromMinTime);
		mTimePickerEnd.setCurrentHour(mEndHourTime);
		mTimePickerEnd.setCurrentMinute(mEndMinTime);
	}

	private void initView() {
		setContentView(R.layout.activity_time_picker);
		((TextView) findViewById(R.id.tv_title)).setText(R.string.time_select);
		mTimePickerStart = (TimePicker) findViewById(R.id.tp_from);
		mTimePickerStart.setIs24HourView(true);
		mTimePickerEnd = (TimePicker) findViewById(R.id.tp_to);
		mTimePickerEnd.setIs24HourView(true);
	}

	private void initListener() {
		mTimePickerStart
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {
						mFromHourTime = hourOfDay;
						mFromMinTime = minute;
						if (minute < 10)
							CameraSet.recodingStartTime = hourOfDay + ":0"
									+ minute;
						CameraSet.recodingStartTime = hourOfDay + ":" + minute;
						mTimePickerEnd.setCurrentHour(mFromHourTime);
						mTimePickerEnd.setCurrentMinute(mFromMinTime);
					}
				});

		mTimePickerEnd
				.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

					@Override
					public void onTimeChanged(TimePicker view, int hourOfDay,
							int minute) {
						// The End Time cannot be lower than start time,
						// if it is, select start time automatic
						if (hourOfDay < mFromHourTime
								|| (hourOfDay == mFromHourTime && minute <= mFromMinTime)) {
							Toast.makeText(TimePickActivity.this,
									R.string.invalide_selected_time,
									Toast.LENGTH_SHORT).show();
							mTimePickerEnd.setCurrentHour(mFromHourTime);
							mTimePickerEnd.setCurrentMinute(mFromMinTime);
							return;
						}
						mEndHourTime = hourOfDay;
						mEndMinTime = minute;
						if (minute < 10)
							CameraSet.recodingEndTime = hourOfDay + ":0"
									+ minute;
						CameraSet.recodingEndTime = hourOfDay + ":" + minute;
					}
				});
	}

	public void finishActivity(View v) {
		finish();
	}
}
